<!--
 - MineAdmin is committed to providing solutions for quickly building web applications
 - Please view the LICENSE file that was distributed with this source code,
 - For the full copyright and license information.
 - Thank you very much for using MineAdmin.
 -
 - @Author X.Mo<root@imoi.cn>
 - @Link   https://gitee.com/xmo/mineadmin-vue
-->
<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <div class="lg:w-2/12 w-full h-full p-2 shadow">
      <ma-tree-slider v-model="depts" searchPlaceholder="搜索分类" :field-names="{ title: 'label', key: 'value' }"
        :selectedKeys="['all']" @click="switchDept" />
    </div>

    <div class="lg:w-10/12 w-full lg:ml-4 mt-5 lg:mt-0">
      <!-- CRUD 组件 -->
      <ma-crud :options="crud" :columns="columns" ref="crudRef">
        <!-- 状态列 -->
        <template #is_show="{ record }">
          <a-switch :checked-value="1" unchecked-value="2" @change="changeStatus($event, record.id)"
            :default-checked="record.is_show == 1" />
        </template>
        <!-- 头像列 -->
        <template #image="{ record }">
          <a-avatar>
            <img :src="record.image || $url + 'avatar.jpg'" style="object-fit: cover" />
          </a-avatar>
        </template>


      </ma-crud>
    </div>

    <a-modal v-model:visible="setHomeVisible" @before-ok="saveHomePage">
      <template #title>设置用户后台首页</template>
      <a-form-item label="用户首页">
        <a-select v-model="homePage" placeholder="请选择用户首页">
          <a-option v-for="(item, index) in homePageList" :key="index" :value="item.key">
            {{ item.title }}
          </a-option>
        </a-select>
      </a-form-item>
    </a-modal>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from 'vue'
import MaTreeSlider from '@cps/ma-treeSlider/index.vue'
import dept from '@/api/kuaili/kuailiCategory'
import user from '@/api/kuaili/kuailiGood'
import commonApi from '@/api/common'
import { Message, Modal } from '@arco-design/web-vue'

const depts = ref([])
const homePageList = ref([])
const crudRef = ref()

const setHomeVisible = ref(false)
const userid = ref()
const homePage = ref('')

onMounted(() => {
  dept.tree().then(res => {
    depts.value = res.data
    depts.value.unshift({ label: '所有分类', value: 'all' })
  })
  commonApi.getDict('dashboard').then(res => homePageList.value = res.data)
})

let isRecovery = computed(() => crudRef.value ? crudRef.value.isRecovery : false)

const switchDept = (id) => {
  crudRef.value.requestParams = id[0] === 'all' ? { category_hash: undefined } : { category_hash: id[0] }
  crudRef.value.requestData()
}

const changeStatus = async (is_show, id) => {
  const response = await user.changeStatus({ id, is_show })
  if (response.success) {
    Message.success(response.message)
  }
}

const updateCache = id => {
  user.clearCache(id).then(res => {
    if (res.success) Message.success(res.message)
  })
}

const resetPassword = (id) => {
  user.initUserPassword({ id }).then(res => res.success && Message.success(res.message))
}

const saveHomePage = (done) => {
  user.setHomePage({ id: userid.value, dashboard: homePage.value }).then(res => {
    res.success && Message.success(res.message)
  })
  done(true)
}

const selectOperation = (value, id) => {
  if (value === 'resetPassword') {
    Modal.info({
      title: '提示',
      content: '确定将该用户密码重置为 123456 吗？',
      simple: false,
      onBeforeOk: (done) => {
        resetPassword(id)
        done(true)
      }
    })
    return
  }

  if (value === 'updateCache') {
    updateCache(id)
    return
  }

  if (value === 'setHomePage') {
    setHomeVisible.value = true
    userid.value = id
    return
  }
}

const crud = reactive({
  api: user.getPageList,
  recycleApi: user.getRecyclePageList,
  searchColNumber: 3,
  showIndex: false,
  pageLayout: 'fixed',
  rowSelection: { showCheckedAll: true },
  operationColumn: true,
  operationWidth: 200,
  add: { show: true, api: user.save, auth: ['system:user:save'] },
  edit: { show: true, api: user.update, auth: ['system:user:update'] },
  delete: {
    show: true,
    api: user.deletes, auth: ['system:user:delete'],
    realApi: user.realDeletes, realAuth: ['system:user:realDeletes']
  },
  recovery: { show: true, api: user.recoverys, auth: ['system:user:recovery'] },
  import: { show: true, url: 'system/user/import', templateUrl: 'system/user/downloadTemplate', auth: ['system:user:import'] },
  export: { show: true, url: 'system/user/export', auth: ['system:user:export'] },
  formOption: {
    width: 800,
    layout: [
      { formType: 'grid', cols: [{ span: 24, formList: [{ dataIndex: 'image' }] }] },
      { formType: 'grid', cols: [{ span: 12, formList: [{ dataIndex: 'name' }] }] },
      { formType: 'grid', cols: [{ span: 24, formList: [{ dataIndex: 'is_show' }] }] },
    ]
  },
  isDbClickEdit: false,
})

const columns = reactive([
  { title: 'ID', dataIndex: 'id', addDisplay: false, editDisplay: false, width: 50, hide: true },
  {
    title: '图片', dataIndex: 'image', width: 75, formType: 'upload',
    type: 'image', rounded: true, labelWidth: '86px'
  },
  {
    title: '名称', dataIndex: 'name', width: 130,  addDisabled: false, editDisabled: true,
    formType: 'input', search: true,
    commonRules: [{ required: true, message: '名称必填' }]
  },

  {
    title: "排序",
    dataIndex: "sort",
    formType: "input-number",
    search: true,
    commonRules: {
      required: true,
      message: "请输入排序"
    },
    min: 0,
    max: 100,
    step: 1,
    precision: 0
  },
  {
    title: "显示/隐藏",
    dataIndex: "is_show",
    formType: "radio",
    search: true,
    commonRules: {
      required: true,
      message: "请输入状态"
    },
    dict: {
      name: "data_status",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    }
  },

])
</script>

<script>
export default { name: 'system:user' }
</script>

<style scoped></style>